package com.se.xb.dao;

import com.se.xb.entity.User;
import com.se.xb.utils.DataSourceUtils;
import com.se.xb.dao.BaseDao;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDao extends BaseDao {

    public User getUserByUsername(String username) {
        String sql = "SELECT * FROM user WHERE username = ?";
        try {
            User user = jdbcTemplate.queryForObject(sql, new Object[]{username}, new BeanPropertyRowMapper<>(User.class));
            return user;
        } catch (Exception e) {
            // 更详细的异常处理
            System.out.println("Error occurred while fetching user: " + e.getMessage());
            // 可以选择记录日志、抛出自定义异常等
            return null; // 或者根据业务需求返回默认值或抛出异常
        }
    }

    public User getUserById(Long id) {
        String sql = "SELECT * FROM user WHERE id = ?";
        try {
            User user = jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(User.class));
            return user;
        } catch (Exception e) {
            // 更详细的异常处理
            System.out.println("Error occurred while fetching user: " + e.getMessage());
            // 可以选择记录日志、抛出自定义异常等
            return null; // 或者根据业务需求返回默认值或抛出异常
        }
    }

    public User getUserByEmail(String email) {
        String sql = "SELECT * FROM user WHERE email = ?";
        try {
            User user = jdbcTemplate.queryForObject(sql, new Object[]{email}, new BeanPropertyRowMapper<>(User.class));
            return user;
        } catch (Exception e) {
            // 更详细的异常处理
            System.out.println("Error occurred while fetching user: " + e.getMessage());
            // 可以选择记录日志、抛出自定义异常等
            return null; // 或者根据业务需求返回默认值或抛出异常
        }
    }

    public void save(User user) {
        String sql = "INSERT INTO user (id, username, password, email,phone) VALUES (NULL,?, ?, ?, ?)";
        try {
            jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone());
        } catch (Exception e) {
            System.out.println("Error occurred while fetching user: " + e.getMessage());
        }
    }

    public void update(User user) {
        String sql = "UPDATE user SET username = ?, password = ?, email = ?, phone = ?,real_name =?,gender=?,age=?,info=?,is_secret=?,pic=?,dept_name=? WHERE username = ?";
        try {
            jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone(), user.getRealName(), user.getGender(), user.getAge(), user.getInfo(), user.getIsSecret(), user.getPic(), user.getDeptName(), user.getUsername());
        } catch (Exception e) {
            System.out.println("Error occurred while fetching user: " + e.getMessage());
        }
    }

    public Integer getFocusCount(Long id) {
        String sql = "select count(*) from userfocus where user_id = ?";
        try {
            Integer focusCount = jdbcTemplate.queryForObject(sql, new Object[]{id}, Integer.class);
            return focusCount;
        } catch (Exception e) {
            // 更详细的异常处理
            System.out.println("Error occurred while fetching user: " + e.getMessage());
            // 可以选择记录日志、抛出自定义异常等
            return null; // 或者根据业务需求返回默认值或抛出异常
        }
    }

    public List<User> getUserList(Integer currentPage, String... query) {
        String sql = "";
        Integer Start = (currentPage - 1) * 5;
        Integer End = 5;
        List<User> users = null;
        if (query == null || query.length == 0) {
            sql = "SELECT * FROM user LIMIT ?, ?";
            try {
                users = jdbcTemplate.query(sql, new Object[]{Start, End}, new BeanPropertyRowMapper<>(User.class));
            } catch (Exception e) {
                // 更详细的异常处理
                System.out.println("Error occurred while fetching user: " + e.getMessage());
                // 可以选择记录日志、抛出自定义异常等
                return null; // 或者根据业务需求返回默认值或抛出异常
            }
        } else {
            sql = "SELECT * FROM user WHERE username LIKE ? LIMIT ?,?";
            try {
                users = jdbcTemplate.query(sql, new Object[]{"%" + query[0] + "%", Start, End}, new BeanPropertyRowMapper<>(User.class));
            } catch (Exception e) {
                // 更详细的异常处理
                System.out.println("Error occurred while fetching user: " + e.getMessage());
                // 可以选择记录日志、抛出自定义异常等
                return null; // 或者根据业务需求返回默认值或抛出异常
            }
        }
        return users;
    }


    public List<User> getUserList() {
        String sql = "SELECT * FROM user ";
        try {
            List<User> users = jdbcTemplate.query(sql,  new BeanPropertyRowMapper<>(User.class));
            return users;
        } catch (Exception e) {
            // 更详细的异常处理
            System.out.println("Error occurred while fetching user: " + e.getMessage());
            // 可以选择记录日志、抛出自定义异常等
            return null; //或者根据业务需求返回默认值或抛出异常
        }
    }

    public Integer getUserCount(String... query) {
        String sql = "";
        if (query == null || query.length == 0) {
            sql = "SELECT COUNT(*) FROM user";
        } else {
            sql = "SELECT COUNT(*) FROM user WHERE username LIKE ?";
        }
        try {
            Integer count = jdbcTemplate.queryForObject(sql, new Object[]{"%" + query[0] + "%"}, Integer.class);
            return count;
        } catch (Exception e) {
            System.out.println("Error occurred while fetching user: " + e.getMessage());
            return null;
        }
    }

    public void focus(Long userId, Long focusId) {
        String sql = "INSERT INTO userfocus (user_id, user_focus_id) VALUES (?, ?)";
        try {
            jdbcTemplate.update(sql, userId, focusId);
        } catch (Exception e) {
            System.out.println("Error occurred while fetching user: " + e.getMessage());
        }
    }

    public void unfocus(Long userId, Long focusId) {
        String sql = "DELETE FROM userfocus WHERE user_id = ? AND user_focus_id = ?";
        try {
            jdbcTemplate.update(sql, userId, focusId);
        } catch (Exception e) {
            System.out.println("Error occurred while fetching user: " + e.getMessage());
        }
    }

    public List<User> getFocusList(Integer currentPage, String... query) {
        String sql = "";
        Integer Start = (currentPage - 1) * 5;
        Integer End = 5;
        List<User> users = null;
        sql = "SELECT * FROM user WHERE id IN (SELECT user_focus_id FROM userfocus WHERE user_id = ?) LIMIT ?, ?";
        try {
            users = jdbcTemplate.query(sql, new Object[]{query[0], Start, End}, new BeanPropertyRowMapper<>(User.class));
        } catch (Exception e) {
            // 更详细的异常处理
            System.out.println("Error occurred while fetching user:" + e.getMessage());
        }
        return users;
    }


    public List<User> getFocusList(Long userId) {
        String sql = "SELECT * FROM user WHERE id IN (SELECT user_focus_id FROM userfocus WHERE user_id = ?)";
        try {
            List<User> users = jdbcTemplate.query(sql, new Object[]{userId}, new BeanPropertyRowMapper<>(User.class));
            return users;
        } catch (Exception e) {
            // 更详细的异常处理
            System.out.println("Error occurred while fetching user: " + e.getMessage());
        }
        return null;
    }
}